﻿using System;
using System.Windows.Forms;

namespace ClientApplication
{
    public partial class Form1 : Form
    {
        private IFirstServiceProxyUnity _UnityFs;
        private IFirstServiceProxyWindsor _WindsorFs;
        private IFirstServiceProxyPostSharp _PostSharpFs;

        public Form1(IFirstServiceProxyUnity unityFs, IFirstServiceProxyWindsor windsorFs, IFirstServiceProxyPostSharp postSharpFs)
        {
            InitializeComponent();

            _UnityFs = unityFs;
            _WindsorFs = windsorFs;
            _PostSharpFs = postSharpFs;
        }

        private static void CallMethodAndShowMessage<T>(Func<string> method) where T : Exception
        {
            try
            {
                MessageBox.Show(method());
            }
            catch (T ex)
            {
                ShowException(ex);
            }
            catch
            {
                MessageBox.Show("Error");
            }
        }

        private static void ShowException(Exception ex)
        {
            MessageBox.Show(string.Format("Error: {0}\n{1}", ex.Message, ex.StackTrace));
        }

        #region Windsor
        private void btnCallFirstServiceHelloWorld_Click(object sender, EventArgs e)
        {
            CallMethodAndShowMessage<Exception>(_WindsorFs.CallHelloWorld);
        }

        private void btnCallUnstableHelloWorld_Click(object sender, EventArgs e)
        {
            CallMethodAndShowMessage<InvalidOperationException>(_WindsorFs.CallUnstableHelloWorld);
        }

        private void btnCallSlowHelloWorld_Click(object sender, EventArgs e)
        {
            CallMethodAndShowMessage<InvalidOperationException>(_WindsorFs.CallSlowHelloWorld);
        }

        private void btnCallSlowerHelloWorld_Click(object sender, EventArgs e)
        {
            CallMethodAndShowMessage<TimeoutException>(_WindsorFs.CallSlowerHelloWorld);
        }

        private void btnCallFirstServiceFailingHelloWorld_Click(object sender, EventArgs e)
        {
            CallMethodAndShowMessage<Exception>(_WindsorFs.CallFailingHelloWorld);
        }
        #endregion

        #region Unity
        private void btnCallHelloWorldUnity_Click(object sender, EventArgs e)
        {
            CallMethodAndShowMessage<Exception>(_UnityFs.CallHelloWorld);
        }

        private void btnCallUnstableHelloWorldUnity_Click(object sender, EventArgs e)
        {
            CallMethodAndShowMessage<InvalidOperationException>(_UnityFs.CallUnstableHelloWorld);
        }

        private void btnCallSlowHelloWorldUnity_Click(object sender, EventArgs e)
        {
            CallMethodAndShowMessage<InvalidOperationException>(_UnityFs.CallSlowHelloWorld);
        }

        private void btnCallSlowerHelloWorldUnity_Click(object sender, EventArgs e)
        {
            CallMethodAndShowMessage<TimeoutException>(_UnityFs.CallSlowerHelloWorld);
        }

        private void btnCallFailingHelloWorldUnity_Click(object sender, EventArgs e)
        {
            CallMethodAndShowMessage<Exception>(_UnityFs.CallFailingHelloWorld);
        }
        #endregion

        #region PostSharp
        private void btnHelloWorldPostSharp_Click(object sender, EventArgs e)
        {
            CallMethodAndShowMessage<Exception>(_PostSharpFs.CallHelloWorld);
        }

        private void btnUnstableHelloWorldPostSharp_Click(object sender, EventArgs e)
        {
            CallMethodAndShowMessage<InvalidOperationException>(_PostSharpFs.CallUnstableHelloWorld);
        }

        private void btnSlowHelloWorldPostSharp_Click(object sender, EventArgs e)
        {
            CallMethodAndShowMessage<InvalidOperationException>(_PostSharpFs.CallSlowHelloWorld);
        }

        private void btnSlowerHelloWorldPostSharp_Click(object sender, EventArgs e)
        {
            CallMethodAndShowMessage<TimeoutException>(_PostSharpFs.CallSlowerHelloWorld);
        }

        private void btnCallFailingHelloWorldPostSharp_Click(object sender, EventArgs e)
        {
            CallMethodAndShowMessage<Exception>(_PostSharpFs.CallFailingHelloWorld);
        }
        #endregion
    }
}
